#include <linux/semaphore.h>
#include <linux/init.h>
#include <linux/module.h>
#include <linux/rwsem.h>

MODULE_LICENSE("GPL");

static int __init down_read_init(void);
static void __exit down_read_exit(void);

#define EXEC_DOWN_WRITE  0
struct rw_semaphore rwsem;

int __init down_read_init(void)
{

	printk("---------------------\n [%s] eneter! \n",__func__);


	init_rwsem( &rwsem );                  //读写信号量初始化
//	printk("after init_rwsem, count: %ld\n", (long int)rwsem.count);
	if( EXEC_DOWN_WRITE )
		down_write( &rwsem );              //写者获取读写信号量
	down_read( &rwsem );                   //读者获取读写信号量
//	printk("first down_read, count: %ld\n", (long int)rwsem.count);
	down_read( &rwsem );
//	printk("second down_read, count: %ld\n", (long int)rwsem.count);

	while( atomic64_read(rwsem.count) != 0 )
	{
		up_read( &rwsem );                 //读者释放读写信号量
	}

	
	printk("---------------------\n [%s] init_over \n",__func__);
	return 0;
}


void __exit down_read_exit(void)
{
	printk("[%s] exit! \n",__func__);
}

module_init(down_read_init);
module_exit(down_read_exit);

